﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Browser;

namespace WOWOGame
{
    public partial class MainPage : UserControl
    {
        DispatcherTimer _DispatcherTimer;
        public MainPage()
        {
            InitializeComponent();
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            Show();
        }

        void Content_Resized(object sender, EventArgs e)
        {
            Close();
            isRePlay = true;
        }
        bool isRePlay = false;
        List<Plume> plumeList;
        void dt_Tick(object sender, EventArgs e)
        {
            foreach (var item in plumeList)
            {
                item.OnFrame();
            }
            if (isDestroy)
            {
                foreach (var item in plumeList)
                    LayoutRoot.Children.Remove(item);
                _DispatcherTimer.Stop();
                _DispatcherTimer.Tick -= dt_Tick;
                _DispatcherTimer = null;
                plumeList.Clear();
                plumeList = null;
                isDestroy = false;
                if (isRePlay)
                    Show();
            }
        }
        private bool isDestroy = false;
        public void Show()
        {
            isDestroy = false;
            _DispatcherTimer = new DispatcherTimer();
            plumeList = new List<Plume>();
            _DispatcherTimer.Interval = TimeSpan.FromMilliseconds(50);
            _DispatcherTimer.Tick += new EventHandler(dt_Tick);
            _DispatcherTimer.Start();
            for (int i = 0; i < 30; i++)
            {
                Plume plume = new Plume();
                plumeList.Add(plume);
                LayoutRoot.Children.Add(plume);
            }
            isRePlay = false;
        }
        public void Close()
        {
            isDestroy = true;            
        }

        private void TextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("http://www.ichinagames.com/"), "__blank");
        }
    }
}
